#! /bin/bash

install_dir="/usr/local/Ascend/ascend-toolkit/latest/opp/vendors/customize/"
script_dir=$(dirname "$(realpath $0)")
upper_op_name="Eye"
lower_op_name="eye"

platform="cpu"
if [ -n "$1" ]; then
    platform="$1"
fi

export ASCEND_SLOG_PRINT_TO_STDOUT=0
export ASCEND_GLOBAL_LOG_LEVEL=3
export ASCEND_LATEST_INSTALL_PATH="/usr/local/Ascend/ascend-toolkit/latest/"

rm -rf ./*.log*
rm -rf ./debug_workspace
rm -rf /usr/local/Ascend/ascend-toolkit/latest/opp/vendors

# 编译并安装包
cd ../../$upper_op_name && rm -rf ./build_out
if [ "$platform" = "simulator" ]; then
    sed -i "s/ascend910b/ascend310b/g" ./CMakePresets.json
    sed -i "s/ascend910b/ascend310b/g" ./op_host/$lower_op_name.cpp
    ./build.sh
    sed -i "s/ascend310b/ascend910b/g" ./CMakePresets.json
    sed -i "s/ascend310b/ascend910b/g" ./op_host/$lower_op_name.cpp
else
    ./build.sh
fi
run_file_name="$(ls ./build_out/custom_opp_*.run)"
if [ -z "$run_file_name" ]; then
    exit 1
fi
bash "$run_file_name"
cd -

if [ "$platform" = "npu" ]; then
    ascendebug kernel --backend "npu" --json-file $lower_op_name.json --chip-version Ascend910B1 \
        --core-type VectorCore --repo-type customize --customize-path $install_dir 2>&1 | tee abc.log
elif [ "$platform" = "simulator" ]; then
    ascendebug kernel --backend "simulator" --json-file $lower_op_name.json --chip-version Ascend310B1 \
        --core-type VectorCore --repo-type customize --customize-path $install_dir \
        --timeout 1800 --block-num 1 2>&1 | tee abc.log
else
    ascendebug kernel --backend "cpu" --json-file $lower_op_name.json --chip-version Ascend910B1 \
        --core-type VectorCore --repo-type customize --customize-path $install_dir  2>&1 | tee abc.log
fi

if [ "$platform" = "simulator" ]; then
    echo -e "==================================================================================\n"
    python3 verify_result.py ./debug_workspace/$upper_op_name/simulator/output/y.bin ./debug_workspace/$upper_op_name/data/y.bin
else
    echo -e "==================================================================================\n"
    cat ./debug_workspace/$upper_op_name/${platform}/output/y.txt
fi

echo -e "==================================================================================\n"
grep "==zf==" *.log

echo -e "\n=================================================================================="
cat gen_bin_golden.log

echo -e "\n=================================================================================="